<?php

namespace app\admin\service;


use app\admin\model\Member;
use think\facade\Db;

/**
 * 用户管理-服务类
 * @author 小树林
 * @since: 2024/11/23
 * Class MemberService
 * @package app\admin\service
 */
class MemberService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new Member();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2024/11/23
     * @author 小树林
     */
    public function getList()
    {
        $param = request()->param();

        //是否为远程搜索查询
        $is_query = $param['_is_query'] ?? '';
        if (!empty($is_query)) {
            return parent::query($param);
        }

        // 查询条件
        $map = [];
        $nickname = getter($param, 'nickname');
        if ($nickname) {
            $arr = Db::name("member")->where("nickname", "like", "%{$nickname}%")->column("id");

            $map[] = ['user_id', 'in', $arr];
        }

        // 有效标识
        $mark = isset($param['mark']) ? (int)$param['mark'] : 0;
        if ($mark) {
            $map[] = ['mark', '=', $mark];
        }

        $field = '*';
        $with = [
            'with',
            [
                'member' => function ($query) {
                    $query->field('id,nickname');
                }
            ]
        ];
        $sort = '';

        $res = parent::getList($map, $field, $with, $sort); // TODO: Change the autogenerated stub
        if ($res['count'] != 0) {
            foreach ($res['data'] as $k=>$v) {
                $res['data'][$k]['avatar'] = [];
                $res['data'][$k]['avatar'][] = get_image_url($v['avatar']);
                //查询是否为店长
                $shop = Db::name("shop_manager")->where("user_id",$v['id'])->find();
                $is_shop = 0;
                if (!empty($shop)){
                    $is_shop = 1;
                }
                $res['data'][$k]['is_shop'] = $is_shop;
            }
        }
        return $res;
    }

    /**
     * 添加或编辑
     * @return array
     * @since 2024/11/23
     * @author 小树林
     */
    public function edit()
    {
        // 参数
        $data = request()->param();

        // 用户头像处理
        $avatar = getter($data, 'avatar');
        if (!empty($avatar)) {
            if (strpos($avatar, "temp") !== false) {
                $data['avatar'] = save_image($avatar, 'member');
            } else {
                $data['avatar'] = str_replace(IMG_URL, "", $avatar);
            }
        }

        return parent::edit($data); // TODO: Change the autogenerated stub
    }


}